package com.springboot.frame.controller;

import com.springboot.frame.mq.RabbitProducer;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class RabbitMqController {

	@Autowired
	private RabbitProducer rabbitProducer;
	@Autowired
	private RabbitTemplate rabbitTemplate;


	/**
	 * 正常消息队列，队列最大长度5
	 */
	@GetMapping("/normalQueue")
	public Map<String, Object> normalQueue() {

		Map<String, Object> map = new HashMap<>();
		map.put("messageId", String.valueOf(UUID.randomUUID()));
		map.put("data", System.currentTimeMillis() + ", 正常队列消息，最大长度 5");

		rabbitTemplate.convertAndSend("normalExchange", "normalRouting", map, new CorrelationData());
		return map;
	}


	/**
	 * 消息 TTL, time to live
	 */
	@GetMapping("/ttlToDead")
	public Map<String, Object> ttlToDead() {

		Map<String, Object> map = new HashMap<>();
		map.put("messageId", String.valueOf(UUID.randomUUID()));
		map.put("data", System.currentTimeMillis() + ", ttl队列消息");

		rabbitTemplate.convertAndSend("normalExchange", "ttlRouting", map, new CorrelationData());
		return map;
	}


	@GetMapping("/sendDemoQueue")
	public Object sendDemoQueue() {
		rabbitProducer.sendDemoQueue();
		return "success";
	}

	@GetMapping("/sendFanout")
	public Object sendFanout() {
		rabbitProducer.sendFanout();
		return "success";
	}

	@GetMapping("/sendTopicTopicAB")
	public Object sendTopicTopicAB() {
		rabbitProducer.sendTopicTopicAB();
		return "success";
	}

	@GetMapping("/sendTopicTopicB")
	public Object sendTopicTopicB() {
		rabbitProducer.sendTopicTopicB();
		return "success";
	}

	@GetMapping("/sendTopicTopicBC")
	public Object sendTopicTopicBC() {
		rabbitProducer.sendTopicTopicBC();
		return "success";
	}
}
